package jjn.round1;

/**
 * @author Jjn
 * @since 2022-10-05 11:00
 */
public class LeetCode12_IntegerToRoman {
    
    //    字符          数值
    //    I             1
    //    V             5
    //    X             10
    //    L             50
    //    C             100
    //    D             500
    //    M             1000
    public String intToRoman(int num) {
        int[] bases = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] names = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        StringBuilder stringBuilder = new StringBuilder();
        while (num > 0) {
            for (int i = 0; i < bases.length; i++) {
                if (num >= bases[i]) {
                    int count = num / bases[i];
                    stringBuilder.append(names[i].repeat(count));
                    num = num - count * bases[i];
                }
            }
        }
        return stringBuilder.toString();
    }
}
